//
//  TXFStatues.swift
//  weibo飞飞
//
//  Created by 田侠飞 on 16/6/27.
//  Copyright © 2016年 田侠飞. All rights reserved.
//

import UIKit

class TXFStatues: NSObject {

    var created_at : String?
      var textAttr: NSMutableAttributedString?
    var text: String?{
    
        didSet{
        textAttr = getWeiBoAttr(text ?? "")
        
        
        }
    
    
    }
    
    var source : String?{
    
        didSet{
        
        
        
        sourceAttr = getSourceString(source)
        
        }
    
    
    }
    
    var id : Int64 = 0
    
    
    
    var user: TXFUser?
    
    
    
    
    
    //转发微博的:
    var retweeted_status: TXFStatues?
    
    
    
    
    //图片:
    
    var pic_urls: [TXFPictureInfo]?
    
    
    var sourceAttr : NSMutableAttributedString?
//    reposts_count	int	转发数
//    comments_count	int	评论数
//    attitudes_count
    var reposts_count : Int = 0{
    
    
        didSet{
        
        repostsStr = getCountString(reposts_count, title: "转发")
        
        
        }
    
    }
    
    var comments_count : Int = 0{
        didSet{
            
            commentsStr = getCountString(comments_count, title: "评论")
            
            
        }
    
    }
    
    var attitudes_count : Int = 0{
    
        didSet{
            attitudesStr = getCountString(attitudes_count, title: "赞")
  
        }
    
    }
    
    
    var repostsStr : String?
    
    var commentsStr : String?
    
    var attitudesStr : String?
    
     init(dict: [String: AnyObject]) {
        
       super.init()
        
        setValuesForKeysWithDictionary(dict)
        
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    override func setValue(value: AnyObject?, forKey key: String) {
        
        if key == "user"{
        
            guard  let v = value as?[String: AnyObject] else{
            
            
            return
            }
        
            let u = TXFUser(dict: v)
                
              user = u
            
            
        }else if key == "retweeted_status"   {
            guard let v = value as?[String: AnyObject] else{
           
            return
            }
            let s = TXFStatues(dict: v)
            retweeted_status = s
        
        }else if key == "pic_urls"      {
        
            guard let v = value as? [[String: AnyObject]] else{
            
            
            return
            }
        
            //字典转模型:
            
            var  tempArray  : [TXFPictureInfo] = [TXFPictureInfo]()
            
            for dict  in  v {
            
            
               let s = TXFPictureInfo(dict: dict)
                
                tempArray.append(s)
            
            }
            
            pic_urls = tempArray
        
        }else{
            super.setValue(value, forKey: key)
            }
       
        
        
    }
 
}
extension TXFStatues{

    func getSourceString(source: String?) ->NSMutableAttributedString{
    
        guard let s = source where s.containsString("\">")else{
        
        return getAttrbuteString("来自 那遥远的地方")
        
        }
    //截取字符串方法:
    let startIndex = s.rangeOfString("\">")!
    let endIndex = s.rangeOfString("</")!
        
    let str = s.substringWithRange(startIndex.endIndex..<endIndex.startIndex)
    
    
    let name = "来自\(str)"
    
    return getAttrbuteString(name)
    
    }


    func getAttrbuteString(name: String) ->NSMutableAttributedString{
    
    //设置文字属性:
        
        let attr = NSMutableAttributedString(string: name)
        
        let range = (name as NSString).rangeOfString("来自")
        let ThemeColor = UIColor.orangeColor()
       attr.addAttribute(NSForegroundColorAttributeName, value: ThemeColor, range: range)
        
        
        
        
    
        return attr
    
    
    
    }



}

extension TXFStatues{


    func  getCountString(count: Int, title: String)->String{
    
        if count <= 0{
        
        return title
        
        }else if count  > 0 && count < 10000{
        return "\(count)"
    
        }else {
        
        let countF = CGFloat(count/1000)/10
            
        var countStr = "\(countF)"
            if countStr.containsString(".0"){
            
            
            countStr = countStr.stringByReplacingOccurrencesOfString(".0", withString: "")
            
            }
            
            return "\(countStr)万"
        
        }
    
    
    
    
    
    }

    func getWeiBoAttr(text: String) -> NSMutableAttributedString{
        // 富文本
        let resultAttr = NSMutableAttributedString(string: text)
        // 定义一个保存模型的数组
        var matchArray:[TXFMatchResult] = [TXFMatchResult]()
        
        // 正则匹配格式
        // 通过正则匹配结果
        (text as NSString).enumerateStringsMatchedByRegex("\\[[a-zA-Z0-9\\u4e00-\\u9fa5]+\\]") { (count, string, range, _) -> Void in
            // 转模型保存数组
            let matchRes = TXFMatchResult(string: (string.memory as! String), range: range.memory)
            // 保存数据
            matchArray.append(matchRes)
            
        }
        
        // 倒序遍历
        for match in matchArray.reverse() {
            let emoticon = TXFEmoticonTools.sharedTools.getSearchEmoticon(match.string)
            // 文本附件
            let att = NSTextAttachment()
            let path = emoticon?.path ?? ""
            let png = emoticon?.png ?? ""
            let name = path + png
            // 设置image
            att.image = UIImage(named: name, inBundle: TXFEmoticonTools.sharedTools.emoticonBundle, compatibleWithTraitCollection: nil)
            // 行高
            let lineHeight = UIFont.systemFontOfSize(15).lineHeight
            // 设置bounds
            att.bounds = CGRect(x: 0, y: -4, width: lineHeight, height: lineHeight)
            
            let attr = NSAttributedString(attachment: att)
            resultAttr.replaceCharactersInRange(match.range, withAttributedString: attr)
        }
        
        //        // 通过正则分割结果
        //        (text as NSString).enumerateStringsSeparatedByRegex("\\[[a-zA-Z0-9\\u4e00-\\u9fa5]+\\]") { (count, string, range, _) -> Void in
        //            print(string.memory)
        //        }
        return resultAttr
    }


}